package collector.support;

import java.util.HashMap;
import java.util.Iterator;

import collector.data.Currency;
import collector.data.Stock;
import collector.data.StockList;
import collector.data.StockMarket;
import collector.data.Time;
import collector.data.Value;
import collector.parsers.StockFinder;
import collector.parsers.StockMarketFinder;
import collector.parsers.StockMarketListFinder;
import collector.parsers.StockMarketSubListFinder;

public class Collector implements Iterable<StockMarket> {

	private HashMap<String, StockMarket> markets;
	private long collectingTime;

	public void run() {
		long starTtime = System.currentTimeMillis();
		currencySupport.init();
		markets = new HashMap<String, StockMarket>();

		StockMarketFinder stockMarketFinder = new StockMarketFinder();
		for (String marketCode : stockMarketFinder) {
			// if (markets.size() == 1)
			// continue;

			System.out.println("Looking at market: " + marketCode);
			StockMarket stockMarket = new StockMarket(marketCode,
					stockMarketFinder.getName(marketCode));
			StockMarketListFinder listFinder = new StockMarketListFinder(
					marketCode);
			for (String listCode : listFinder) {
				System.out.println("Looking at list: " + listCode + "");
				StockMarketSubListFinder stockMarketSubListFinder = new StockMarketSubListFinder(
						marketCode, listCode);
				StockFinder stockFinder = null;
				if (stockMarketSubListFinder.getSubLists().size() == 0) {
					stockFinder = new StockFinder(marketCode, listCode);
					StockList stockList = lookAtStocks(stockFinder, listCode);
					stockMarket.addList(stockList);
					System.out.println();
				} else {
					for (String delimiter : stockMarketSubListFinder) {
						stockFinder = new StockFinder(marketCode, listCode,
								delimiter);
						System.out.print(delimiter + ": ");
						StockList stockList = lookAtStocks(stockFinder,
								listCode);
						stockMarket.addList(stockList);
						System.out.println();
					}
				}
				
				//Debug
				//if (stockMarket.getLists().size() > 1)
				//	break;
			}
			markets.put(stockMarket.getName(), stockMarket);
			
			System.out.println();
			//debug
			//break;
		}
		this.collectingTime = (System.currentTimeMillis() - starTtime) / 1000;
	}

	private StockList lookAtStocks(StockFinder stockFinder, String listCode) {
		StockList stockList = new StockList(listCode);
		for (HashMap<String, String> stockDetails : stockFinder) {
			try {
				String name = stockDetails.get("name");
				if (stockList.size() > 0)
					System.out.print(", ");
				System.out.print("\"" + name + "\"");
				Currency currency = currencySupport.getCurrencySEK(stockDetails
						.get("currency"));
				Value value = new Value(Double.parseDouble(stockDetails
						.get("value")), currency);
				Value highest = new Value(Double.parseDouble(stockDetails
						.get("high")), currency);
				Value lowest = new Value(Double.parseDouble(stockDetails
						.get("low")), currency);
				Time time = new Time(stockDetails.get("time"));
				Value change = new Value(Double.parseDouble(stockDetails
						.get("change")), currency);
				double changeProc = Double.parseDouble(stockDetails
						.get("changeProc"));
				String url = stockDetails.get("url");
				String shortName = stockDetails.get("shortname");
				Stock stock = new Stock(name, value, highest, lowest, time,
						change, changeProc, url, currency, shortName, listCode);
				stockList.addStock(stock);
			} catch (Exception e) {
				//May just be an errornous value at Avanza
				e.printStackTrace();
			}
		}
		return stockList;
	}

	public void printStats() {
		System.out.println("Stock markets: " + markets.size());
		for (StockMarket market : markets.values()) {
			System.out.println(market.getName() + ": "
					+ market.getStocks().size() + " stocks.");
		}
		System.out.println("Time spend collecting: " + collectingTime + "s");
	}

	@Override
	public Iterator<StockMarket> iterator() {
		return markets.values().iterator();
	}

}
